/*!
 * 面板控件.
 * ecp.qzz.ui.button.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.window","qzz.button"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui");
	/**
	 * 面板控件.
	 */
	qzz.ui.toolbutton = qzz.Class(qzz.ui.window, {
		/**
		 * 创建函数
		 */
	    _create:function() {
	    	this.base();
            this.className = "toolButton";
            this.getOption().column = 
            	this.getNumberValue(this.getOption().column, 5);
            this.getOption().buttonList 
                = this.getOption().buttonList || [];
            this.getOption().height = this.getNumberValue(this.getOption().height, 20);
            this.getOption().buttonHeight 
                = this.getNumberValue(this.getOption().buttonHeight, this.getOption().height);
            this.getOption().buttonWidth 
                = this.getNumberValue(this.getOption().buttonWidth, this.getOption().buttonHeight);
            this.getOption().borderWidth = this.getNumberValue(this.getOption().borderWidth, 0);
            this.getOption().space = this.getNumberValue(this.getOption().space, 0);
            this._buttons = {};
	    },
	    /**
	     * 渲染.
	     */
	    _render:function() {	    	
	    	//兼容用Input配置的情况.
	    	if(this.control && this.control[0].tagName === "INPUT") {
	    		var conDiv = $(this.createElement("div"));
	    		conDiv.attr("dataField", this.control.attr("dataField"));
	    		conDiv.attr("data-option", this.control.attr("data-option"));
	    		conDiv.attr("id", this.control.attr("id"));
	    		conDiv.attr("name", this.control.attr("name"));
	    		conDiv.attr("width", this.control.attr("width"));
	    		this.control.replaceWith(conDiv);
	    		this.control = conDiv;
	    		this.control.data("_ecpUI", this);
	    		this.thisWindow = conDiv[0];
	    	}
	    	this.base();
	    	if(this.thisWindow.parentNode != null && this.thisWindow.parentNode.style.position !== "") {
	    	    this.thisWindow.style.position = "absolute";
	    	}
	    	this.thisWindow.style.borderWidth = "0px";
	    	
	    	this._cTable = this.createElement("TABLE");
	    	var tr = this._cTable.insertRow(0);
	    	
	    	this.thisWindow.appendChild(this._cTable);
	    	
	    	this.refresh();
	    },
	    setItemList:function(items) {
	    	this.clearButton();
	    	this.getOption().buttonList = items;
	    	this.refresh();
	    },
	    clearButton:function() {
	    	for(var key in this._buttons) {
	    		this._buttons[key].destroy();
	    	}
	    	this._buttons = {};
	    	for(var i = this._cTable.rows.length - 1; i >= 0; i--) {
	    		var tr = this._cTable.rows[i];
	    		for(var j = tr.cells.length - 1; j >= 0; j--) {
	    			tr.deleteCell(j);
	    		}
	    		this._cTable.deleteRow(i);
	    	}
	    	this._cTable.insertRow(0);
	    },
	    /**
	     * 刷新.
	     */
	    refresh:function() {
	    	var i = 0, r = 0, c = 0, mwidth=0, swidth = 0;
	    	var len = this.getOption().buttonList.length;
	    	var mheight = this.getOption().buttonHeight || this.getOption().height;
	    	var tmh = 0;
	    	this._buttons = {};
	    	var tr = null;
	    	while(i < len) {	    		
	    		if(i % this.getOption().column == 0) {
	    			if(r > 0 && this.getOption().space > 0) {
	    				tr = this._cTable.rows[r];
	    				if(tr == null) {
	    					tr = this._cTable.insertRow(r);
	    				}
	    				//如果有第二行则全添加cell
		    			if(r > 0) {
			    			for(var j = 0; j < this.getOption().column; j++) {
			    				if(tr.cells[j] == null) {
			    					tr.insertCell(j);
			    				}
			    			}
		    			}
		    			tr.style.height = this.getOption().space + "px";
		    			tmh += this.getOption().space;
		    			r++;
	    			}
	    			tr = this._cTable.rows[r];
	    			if(tr == null) {
	    			    tr = this._cTable.insertRow(r);	
	    			}
	    			//如果有第二行则全添加cell
	    			if(r > 0) {
		    			for(var j = 0; j < this.getOption().column; j++) {
		    				if(tr.cells[j] == null) {
		    					tr.insertCell(j);
		    				}
		    			}
	    			}
	    			if(mwidth < swidth) {
	    				mwidth = swidth;
	    			}
	    			tmh += mheight + 1;
	    			swidth = 0;
	    			c = 0;
	    			r++;
	    		}
	    		if(c > 0 && this.getOption().space > 0) {
	    			td = tr.cells[c];
	    			if(td == null) {
	    				td = tr.insertCell(c);
	    			}
	    			td.style.width = this.getOption().space + "px";
	    			swidth += this.getOption().space;
	    			c++;
	    		}
	    		var td = tr.cells[c];
	    		if(td == null) {
	    			td = tr.insertCell(c);
	    		}
	    		c++
	    		var btnItem = this.getOption().buttonList[i];
	    		if(td._btn == null) {
	    			td._btn = new qzz.ui.button(null, {width:this.getOption().buttonWidth,
		    				                           height:this.getOption().buttonHeight,
		    				                           borderWidth:this.getNumberValue(btnItem.borderWidth, 
		    				                        		   this.getOption().borderWidth),
		    				                           style: this.getOption().style || "V"
		    			                        });
	    		}
	    		var btn = td._btn;
	    		btn.setParent(td);
	    		btn.setName(btnItem.name);
	    		this._buttons[btnItem.name] = btn;
	    		if(btnItem.caption != null) {
	    			btn.getOption().caption = btnItem.caption;
	    		}
	    		if(btnItem.icon != null) {
	    			btn.setIcon(btnItem.icon);
	    		}
	    		if(btnItem.hint != null) {
	    			btn.getOption().hint = btnItem.hint;
	    		}
	    		var _this = this;
	    		btn.bind("onClick", function() {
	    			if(this.isenable()) {
	    			    _this.trigger("on" + this.getName(), [this]);
	    			    _this.trigger("onButtonClick", [this]);
	    			}
	    		});
	    		//宽
	    		if(btnItem.width != null) {
	    			btn.setWidth(btnItem.width);
	    			swidth += btnItem.width;
	    		} else {
	    			swidth += this.getOption().buttonWidth;
	    		}
	    		//样式
	    		if(btnItem.common != null) {
	    			btn.clearStyle();
	    			btn.addStyle(btnItem.common);
	    		}
	    		btn.refresh();
	    		i++;
	    	}//while
	    	if(mwidth < swidth) {
				mwidth = swidth;
			}
	    	this.setWidth(mwidth);
	    	
	    	this.setHeight(tmh);
	    	//渲染后事件.
	    	this.trigger("onItemDraw", [this]);
	    },
	    /**
	     * 启用.
	     */
	    enable:function(name) {
	    	if(name != null) {
	    		var btn = this._buttons[name];
	    		if(btn != null) {
	    			btn.enable();	    			
	    		}
	    	} else {
	    		this.base();
	    	}	    	
	    },
	    /**
	     * 禁用.
	     */
	    disable:function(name) {
	    	if(name != null) {
	    		var btn = this._buttons[name];
	    		if(btn != null) {
	    			btn.disable();
	    		}
	    	} else {
	    		this.base();
	    	}
	    },
	    /**
	     * 是否可用.
	     */
	    isenable:function(name) {
	    	if(name != null) {
	    		var btn = this._buttons[name];
	    		if(btn != null) {
	    			return btn.isenable();
	    		} else {
	    			return true;
	    		}	    		
	    	} else {
	    		return this.base();
	    	}
	    },
	    /**
	     * 返回具体按钮对象.
	     */
	    getButton:function(name) {
	    	if(name = null) {
	    		return this._buttons;
	    	} else {
	    	    return this._buttons[name];
	    	}
	    }
	});
	
	/**
	 * 按钮控件.
	 */
	_bindRenderFunc("qzztoolbutton", qzz.ui.toolbutton);
	return qzz.ui.toolbutton;
});
